open Sdl;
open Sdlvideo;

value pix_size = 10;
value draw screen map () =
  let draw_pix mx my color =
    for y = my*pix_size to my*pix_size + (pix_size-1) do
      for x = mx*pix_size to mx*pix_size + (pix_size-1) do
(*         Printf.printf "%dx%dx%d\n" x y color *)
        put_pixel screen ~x ~y (Int32.of_int color);
      done
    done
  in
  (
    let (width,height) = map#size in
    for j=0 to height-1 do  
      for i=0 to width-1 do 
        draw_pix i j begin
          match map#cell (i,j) with
          [ `Block -> 0x000000
          | `Occupation [] -> 0xFFFFFF
          | `Occupation lst when MList.for_all (fun a -> a#state = `Die) lst -> 0xFF0000
          | `Occupation lst when MList.for_all (fun a -> a#state = `Stun or a#state = `Die) lst -> 0xFFFF00
          | `Occupation [a] when a#state = `Run -> 0x00FF00
          | `Occupation [a] when a#state = `Walk -> 0x0000FF
          | `Occupation lst -> 
            match MList.find_all (fun a -> a#state = `Run) lst with
            [ [] ->
              match MList.find_all (fun a -> a#state = `Walk) lst with
              [ [] -> assert False
              | _ -> 0x0000FF
              ]
            | _ ->
              match MList.find_all (fun a -> a#state = `Walk) lst with
              [ [] -> 0x00FF00
              | _ -> assert False
              ]
            ]
          ]
        end
      done
    done;
    flip screen;
  );

